package com.hydra.file.controller;

import com.hydra.base.controller.BaseController;
import com.hydra.base.entity.IdEntity;
import com.hydra.base.entity.UserTimeEntity;
import com.hydra.base.error.BusinessException;
import com.hydra.base.error.ValidateException;
import com.hydra.base.utils.BeanConvertUtils;
import com.hydra.base.utils.SnowIdUtil;
import com.hydra.file.dto.SkylerRecordQueryDto;
import com.hydra.file.entity.SkylerRecord;
import com.hydra.file.entity.SysFile;
import com.hydra.file.mapper.SkylerRecordMapper;
import com.hydra.file.services.FileService;
import com.hydra.file.services.SkylerRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Slf4j
@RestController
@RequestMapping(path = "/api/skyler/record")
public class SkylerRecordController extends BaseController<SkylerRecordQueryDto, SkylerRecord, SkylerRecordMapper, SkylerRecordService> {

    @Resource
    private FileService fileService;

    @Autowired
    public SkylerRecordController(SkylerRecordService repository){
        super.setRepository(repository);
    }

    /**保存数据之前的操作*/
    @Override
    protected SkylerRecord handleBeforeSave(SkylerRecord data, SkylerRecord history) {
        if(data.getCode() == null){
            data.setCode(SnowIdUtil.getInstance().nextId() + "");
        }
        return data;
    }

    /**
     * 批量提交
     */
    @PostMapping(path = {"/insertBatch"})
    public SkylerRecord insertBatch(@RequestBody SkylerRecord t) {
        if (t == null) {
            throw new ValidateException("保存对象不能为空！");
        } else {
            Date recordTime = t.getRecordTime();
            if(recordTime == null){
                throw new ValidateException("记录日期不能为空！");
            }
            String imageId = t.getImageId();
            if(StringUtils.isBlank(imageId)){
                throw new ValidateException("上传图片不能为空！");
            }
            String[] imageIds = imageId.split(",");
            for (String imgId : imageIds) {
                SkylerRecord newData = new SkylerRecord();
                BeanUtils.copyProperties(t, newData);
                newData.setId(null);
                newData.setImageId(imgId);
                newData = this.handleBeforeSave(newData, null);
                this.repository.save(newData);
                t.setId(newData.getId());
            }
            return t;
        }
    }

    /**
     * 按日期分组获取数据
     */
    @GetMapping(path = {"/listRecordGroup"})
    public List<SkylerRecord> listRecordGroup() {
        return this.repository.listRecordGroup();
    }

    /**
     * 根据日期查询数据
     */
    @GetMapping(path = {"/listRecordByDate"})
    public List<SkylerRecord> listRecordByDate(@RequestParam("recordTime") String recordTime) {
        return this.repository.listRecordByDate(recordTime);
    }


    /**
     * 批量删除记录
     */
    @RequestMapping(path = "deleteRecordByDate")
    public boolean deleteRecordByDate(String date) {
        if(StringUtils.isBlank(date)){
            throw new ValidateException("删除日期不能为空！");
        }
        List<SkylerRecord> skylerRecords = this.repository.listRecordByDate(date);
        for (SkylerRecord record : skylerRecords) {
            String imageId = record.getImageId();
            log.debug("删除的文件id："+imageId);
            fileService.deleteSysFile(Long.valueOf(imageId));

            this.repository.removeById(record);
        }
        return true;
    }

    /**
     * 删除记录
     * @param id    记录id
     * @return      返回响应结果为boolean，若抛异常，则code为500
     */
    @RequestMapping(path = "deleteRecord")
    public Long deleteRecord(Long id) {
        //获取记录
        Optional<SkylerRecord> optional = this.repository.getOptById(id);
        if(!optional.isPresent()){
            throw new BusinessException("获取记录失败！");
        }

        SkylerRecord record = optional.get();
        String imageId = record.getImageId();
        log.debug("删除的文件id："+imageId);
        fileService.deleteSysFile(Long.valueOf(imageId));

        this.repository.removeById(record);

        return record.getId();
    }

}
